package JUC;

/**
 * @author 豆包LU
 * @date 2023/5/6 16:04
 */

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

public class ArrayListExample {
    public static void main(String[] args) {
/*//        List<String> list = Collections.synchronizedList(new ArrayList<String>());
//        List<String> list = new ArrayList<>();
//          List<String> list = new Vector<>();
        List<String> list = new CopyOnWriteArrayList<>();
        // 创建并启动10个线程，每个线程向ArrayList添加100个元素
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int j = 0; j < 100; j++) {
                    // 使用synchronized关键字确保多线程访问时的安全性
//                    synchronized (list) {
                        list.add(Thread.currentThread().getName() + " - " + j);
                        System.out.println(Thread.currentThread().getName() +"\t" + list);
//                    }
                }
            }).start();
        }*/

    }

    private static void setNoSafe() {
        Set<String> set = new CopyOnWriteArraySet<>();
        for (int i = 1; i <= 30; i++) {
            new Thread(()->{
                set.add(UUID.randomUUID().toString().substring(0,8));
                System.out.println(Thread.currentThread().getName() +"\t" +
                        set);
            }, String.valueOf(i)).start();
        }
    }
}
